package com.example.leave.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 全局响应类
 *
 * @param <T>
 * @author huangguibo
 */
@ApiModel(value = "CommonResult", description = "返回对象")
public class CommonResult<T> {

    @ApiModelProperty(value = "是否成功")
    private boolean success;

    @ApiModelProperty(value = "状态编码")
    private String code;

    @ApiModelProperty(value = "处理结果描述")
    private String msg;

    @ApiModelProperty(value = "返回的对象信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> CommonResult<T> success() {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(true);
        commonResult.setMsg("success");
        commonResult.setData(null);
        return commonResult;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> CommonResult<T> success(String msg) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(true);
        commonResult.setMsg(msg);
        commonResult.setData(null);
        commonResult.setCode("200");
        return commonResult;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> CommonResult<T> success(T obj) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(true);
        commonResult.setMsg("success");
        commonResult.setData(obj);
        commonResult.setCode("200");
        return commonResult;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> CommonResult<T> success(T obj, String msg) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(true);
        commonResult.setMsg(msg);
        commonResult.setData(obj);
        commonResult.setCode("200");
        return commonResult;
    }

    /**
     * 处理成功返回
     *
     * @return
     */
    public static <T> CommonResult<T> error(T obj, String msg) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(false);
        commonResult.setMsg(msg);
        commonResult.setData(obj);
        return commonResult;
    }

    /**
     * 处理异常返回
     *
     * @param msg
     * @return
     */
    public static <T> CommonResult<T> error(String msg) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(false);
        commonResult.setMsg(msg);
        commonResult.setData(null);
        return commonResult;
    }

    /**
     * 处理警告返回
     *
     * @param msg
     * @return
     */
    public static <T> CommonResult<T> warn(String msg) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.setSuccess(false);
        commonResult.setCode("warn");
        commonResult.setMsg(msg);
        commonResult.setData(null);
        return commonResult;
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String isCode() {
        return code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
